package com.tywj.buscustomerapp.view.activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.tywj.buscustomerapp.R;
import com.tywj.buscustomerapp.model.bean.BuyedBean;
import com.tywj.buscustomerapp.model.bean.DateJoe;
import com.tywj.buscustomerapp.model.bean.RequestBean;
import com.tywj.buscustomerapp.model.bean.StationBean;
import com.tywj.buscustomerapp.model.bean.TravelLineBean;
import com.tywj.buscustomerapp.model.bean.YuPiaoBean;
import com.tywj.buscustomerapp.presenter.BasePresenter;
import com.tywj.buscustomerapp.presenter.BuyTicketPresenter;
import com.tywj.buscustomerapp.presenter.contract.BuyTicketContract;
import com.tywj.buscustomerapp.utils.SPUtils;
import com.tywj.buscustomerapp.utils.TaylorClickLinstener;
import com.tywj.buscustomerapp.utils.TimeRender;
import com.tywj.buscustomerapp.utils.ToastUtils;
import com.tywj.buscustomerapp.utils.myadapter.CommonRcvAdapter;
import com.tywj.buscustomerapp.utils.myadapter.item.AdapterItem;
import com.tywj.buscustomerapp.view.item.DateItem;
import com.tywj.buscustomerapp.view.myview.LoadingDialog;
import com.tywj.buscustomerapp.view.myview.ToastJoe;
import com.umeng.analytics.MobclickAgent;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;

public class BuyTicketActivity extends BaseActivityMvp implements BuyTicketContract.BuyTicketView{
    @BindView(R.id.head_left) ImageView back;
    @BindView(R.id.head_title) TextView title;
    @BindView(R.id.dateRecycler) RecyclerView dateRecycler;
//    @BindView(R.id.buyticket_checkbox1) CheckBox box1;
//    @BindView(R.id.buyticket_checkbox2) CheckBox box2;
    @BindView(R.id.ticket_once) LinearLayout onceTicketLayout;
    @BindView(R.id.ticket_once_about) TextView onceTicketAbout;
//    @BindView(R.id.ticket_month) LinearLayout monthTicketLayout;
    @BindView(R.id.location_start) LinearLayout locationStart;
    @BindView(R.id.location_end) LinearLayout locationEnd;
    @BindView(R.id.check_ticktes) TextView checkTicketNumber;
    @BindView(R.id.buyticket_checkline1) LinearLayout checkLine1;
//    @BindView(R.id.buyticket_checkline2) LinearLayout checkLine2;
    @BindView(R.id.start) TextView start;
    @BindView(R.id.end) TextView end;
    @BindView(R.id.line_gry) View lineGry;
    @BindView(R.id.total_price) TextView total_price;
    @BindView(R.id.submit) TextView submit;
//    @BindView(R.id.month_price) TextView month_price;
//    @BindView(R.id.once_price) TextView once_price;
//    @BindView(R.id.use_time) TextView use_time;
//    @BindView(R.id.month_ticket_name) TextView month_name;
    @BindView(R.id.month_ticket_time) TextView month_time;

    @BindView(R.id.add) ImageView add;
    @BindView(R.id.low) ImageView low;
    @BindView(R.id.per_number) TextView perNumber;

    private BuyTicketContract.BuyTicketPresenter presenter = new BuyTicketPresenter(this);
    private OptionsPickerView tvStart;
    private OptionsPickerView tvEnd;
    private CommonRcvAdapter<DateJoe> adapter;
    private List<DateJoe> dateJoes = new ArrayList<>();
    private List<DateJoe> checkTickets=new ArrayList<>();
    private TravelLineBean travelLineBean;
    private DateItem item;
    private List<StationBean> stations = new ArrayList<>();
    private List<String> startStations = new ArrayList<>();
    private List<String> startStationsId = new ArrayList<>();
    private List<String> endStations = new ArrayList<>();
    private List<String> endStationsId = new ArrayList<>();
    private String chooseStart ="";
    private String chooseStartId ="";
    private String chooseEnd ="";
    private String chooseEndId ="";
    private LoadingDialog waitDialog;
    private int postition = 0;
    private String price = "0";
    private boolean isMonthTicket = false;
    public static BuyTicketActivity activity;
    private int number = 1;
    private int minNumber = 1;


    private void drewDialog() {
        tvStart = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {

            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                if (startStations.size()>0){
                    chooseStart = startStations.get(options1);
                    chooseStartId = startStationsId.get(options1);
                    postition = options1;
                    start.setText(chooseStart);
                    if (chooseEnd!=null && !chooseEnd.equals("")){
                        if (endStationsId.contains(chooseStartId) && (endStationsId.indexOf(chooseStartId)>=endStationsId.indexOf(chooseEndId))){
                            chooseEnd = "";
                            chooseEndId= "";
                            end.setText(R.string.ticket_end);
                            ClearEndStation();
                            price = "0";
                            checkTickets.clear();
                            ChangeToatalMoney();
                            dateJoes = TimeRender.getThisWeekDate(new Date());
                            adapter.setData(dateJoes);
                            adapter.notifyDataSetChanged();
                        }else{
                            presenter.startGetPrice(travelLineBean.getNumber(), chooseStartId, chooseEndId);
                        }
                    }
                }
            }
        }).setSubCalSize(15)
                .isDialog(true)
                .setSubmitColor(getResources().getColor(R.color.appColor))//确定按钮文字颜色
                .setCancelColor(getResources().getColor(R.color.gray1))//取消按钮文字颜色
                .build();
        tvEnd = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                if (!(options1>=endStations.size()) &&endStations.get(options1)!=null) {
                    chooseEnd = endStations.get(options1);
                    chooseEndId = endStationsId.get(options1);
                    end.setText(chooseEnd);
                    presenter.startGetPrice(travelLineBean.getNumber(), chooseStartId, chooseEndId);
                }else{
                    chooseEnd = endStations.get(endStations.size()-1);
                    chooseEndId = endStationsId.get(endStationsId.size()-1);
                    end.setText(chooseEnd);
                    presenter.startGetPrice(travelLineBean.getNumber(), chooseStartId, chooseEndId);
                }
            }
        }).setSubCalSize(15)
                .isDialog(true)
                .setSubmitColor(getResources().getColor(R.color.appColor))//确定按钮文字颜色
                .setCancelColor(getResources().getColor(R.color.gray1))//取消按钮文字颜色
                .build();
        Dialog mDialog = tvStart.getDialog();
        Dialog mDialog1 = tvEnd.getDialog();
        if (mDialog != null && mDialog1!=null) {
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    Gravity.BOTTOM);
            params.leftMargin = 0;
            params.rightMargin = 0;
            tvStart.getDialogContainerLayout().setLayoutParams(params);
            tvEnd.getDialogContainerLayout().setLayoutParams(params);

            Window dialogWindow = mDialog.getWindow();
            if (dialogWindow != null) {
                dialogWindow.getDecorView().setPadding(0, 0, 0, 0);
                WindowManager.LayoutParams lp = dialogWindow.getAttributes();
                lp.width = WindowManager.LayoutParams.MATCH_PARENT;
                lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
                dialogWindow.setAttributes(lp);
                dialogWindow.setWindowAnimations(com.bigkoo.pickerview.R.style.picker_view_slide_anim);//修改动画样式
                dialogWindow.setGravity(Gravity.BOTTOM);//改成Bottom,底部显示
            }
            Window dialogWindow1 = mDialog1.getWindow();
            if (dialogWindow1 != null) {
                dialogWindow1.getDecorView().setPadding(0, 0, 0, 0);
                WindowManager.LayoutParams lp = dialogWindow1.getAttributes();
                lp.width = WindowManager.LayoutParams.MATCH_PARENT;
                lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
                dialogWindow1.setAttributes(lp);
                dialogWindow1.setWindowAnimations(com.bigkoo.pickerview.R.style.picker_view_slide_anim);//修改动画样式
                dialogWindow1.setGravity(Gravity.BOTTOM);//改成Bottom,底部显示
            }
        }

        tvStart.setPicker(startStations, null, null);
        tvEnd.setPicker(endStations, null, null);

    }

    private void drewChceckBox() {
        /**
         * 选择单次票
         */
//        box1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                if (isChecked){
//                    isMonthTicket = false;
//                    box2.setChecked(!isChecked);
//                    box1.setClickable(false);
//                    box2.setClickable(true);
//                    monthTicketLayout.setVisibility(View.GONE);
//                    onceTicketAbout.setVisibility(View.VISIBLE);
//                    onceTicketLayout.setVisibility(View.VISIBLE);
//                    checkTicketNumber.setVisibility(View.VISIBLE);
//                    lineGry.setVisibility(View.VISIBLE);
//                    ChangeToatalMoney();
//                }
//            }
//        });
        /**
         * 选择月票
         */
//        box2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                if (isChecked){
//                    if (chooseStart.equals("")){
//                        ToastJoe.getmToastJoe().ToastShow(BuyTicketActivity.this,null,"请选择上车站点");
//                        box1.setChecked(true);
//                        box2.setChecked(false);
//                    }else if (chooseEnd.equals("")){
//                        box1.setChecked(true);
//                        box2.setChecked(false);
//                        ToastJoe.getmToastJoe().ToastShow(BuyTicketActivity.this,null,"请选择下车站点");
//                    }else if (endStations.contains(chooseStart)){
//                        box1.setChecked(true);
//                        box2.setChecked(false);
//                        ToastJoe.getmToastJoe().ToastShow(BuyTicketActivity.this,null,"上车站点不能再下车站点之后");
//                        end.setText("");
//                        chooseEnd="";
//                    }else{
//                        isMonthTicket = true;
//                        box1.setChecked(!isChecked);
//                        box2.setClickable(false);
//                        box1.setClickable(true);
//                        monthTicketLayout.setVisibility(View.VISIBLE);
//                        onceTicketAbout.setVisibility(View.GONE);
//                        onceTicketLayout.setVisibility(View.GONE);
//                        checkTicketNumber.setVisibility(View.GONE);
//                        lineGry.setVisibility(View.GONE);
//                        ChangeToatalMoney();
//                    }
//
//                }
//            }
//        });
//        box1.setChecked(true);
//        box1.setClickable(false);
    }

    private void drewDatePicket() {
        adapter = new CommonRcvAdapter<DateJoe>(dateJoes) {
            @NonNull
            @Override
            public AdapterItem createItem(Object type) {
                item = new DateItem();
                item.setListener(new DateItem.OnCheckListener() {
                @Override
                public void onChecked(DateJoe dateJoe,int pos,View view) {
                    if (chooseStartId.equals("")){
                        ToastJoe.getmToastJoe().ToastShow(BuyTicketActivity.this,null,"请选择上车站点");
                        dateJoe.isCheck = false;
                        adapter.notifyItemChanged(pos,dateJoe);
                    }else if (chooseEndId.equals("")){
                        ToastJoe.getmToastJoe().ToastShow(BuyTicketActivity.this,null,"请选择下车站点");
                        dateJoe.isCheck = false;
                        adapter.notifyItemChanged(pos,dateJoe);
                    }
//                    else if (endStations.contains(chooseStart)){
//                        ToastJoe.getmToastJoe().ToastShow(BuyTicketActivity.this,null,"上车站点不能再下车站点之后");
//                        end.setText("");
//                        chooseEnd="";
//                        dateJoe.isCheck = false;
//                        adapter.notifyItemChanged(pos,dateJoe);
//                    }
                    else{
                        // TODO: 2018/7/26
                        presenter.startCheckTicket(pos,dateJoe,travelLineBean.getNumber(),chooseStartId,chooseEndId);
                    }
                }

                @Override
                public void onDeleted(DateJoe dateJoe) {
                    checkTickets.remove(dateJoe);
                    ChangeToatalMoney();
                }
            });
                return item;
            }
        };
        RecyclerView.LayoutManager layoutManager = new GridLayoutManager(this,7);
        dateRecycler.setLayoutManager(layoutManager);
        dateRecycler.setAdapter(adapter);
        dateJoes = TimeRender.getThisWeekDate(new Date());
//        adapter.setData(dateJoes);
//        adapter.notifyDataSetChanged();
        presenter.getDays(travelLineBean.getNumber());

    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_but_ticket;
    }

    @Override
    public void initView() {
        title.setText("购票");
        activity = this;
        back.setOnClickListener(this);
        locationStart.setOnClickListener(this);
        locationEnd.setOnClickListener(this);
//        checkLine2.setOnClickListener(this);
        checkLine1.setOnClickListener(this);
        add.setOnClickListener(this);
        low.setOnClickListener(this);
        onceTicketAbout.setOnClickListener(this);
        perNumber.setText(number+"");
        if (getIntent()!=null){
            travelLineBean = (TravelLineBean) getIntent().getSerializableExtra("lineNum");
            presenter.startLoadStations(travelLineBean.getNumber());
            drewDatePicket();
            drewChceckBox();
            drewDialog();

//            chooseStart = startStations.get(0);
//            chooseStartId = startStationsId.get(0);
//            start.setText(chooseStart);
        }
        String[] thisTime = TimeRender.getFullDateStr9();
//        month_name.setText(thisTime[1]+"月月票");
        month_time.setText(thisTime[0]+"年"+thisTime[1]+"月");
        if (thisTime[1].equals("1") || thisTime[1].equals("3")|| thisTime[1].equals("5")|| thisTime[1].equals("7")
                || thisTime[1].equals("8")|| thisTime[1].equals("10")|| thisTime[1].equals("12")){
//            use_time.setText(thisTime[0]+"年"+thisTime[1]+"月"+01+"日——"+thisTime[0]+"年"+thisTime[1]+"月"+31+"日");
        }else if (thisTime[1].equals("2")){
            int year= Integer.valueOf(thisTime[0]);
            if ((year%4==0 && year%100!=0) || (year%400==0)){
//                use_time.setText(thisTime[0]+"年"+thisTime[1]+"月"+01+"日——"+thisTime[0]+"年"+thisTime[1]+"月"+29+"日");
            }else{
//                use_time.setText(thisTime[0]+"年"+thisTime[1]+"月"+01+"日——"+thisTime[0]+"年"+thisTime[1]+"月"+28+"日");
            }
        }else{
//            use_time.setText(thisTime[0]+"年"+thisTime[1]+"月"+01+"日——"+thisTime[0]+"年"+thisTime[1]+"月"+30+"日");
        }

        perNumber.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                ChangeToatalMoney();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    public void initData() {
        submit.setOnClickListener(new TaylorClickLinstener() {
            @Override
            public void onMultiClick(View v) {
                if (!isMonthTicket) {
                    //购买单程票
                    if (checkTickets.size() < 1) {
                        ToastJoe.getmToastJoe().ToastLongShow(BuyTicketActivity.this, null, "请选择乘车日期");
                    } else if (price.equals("0")) {
                        presenter.startGetPrice(travelLineBean.getNumber(), chooseStartId, chooseEndId);
                    } else {
//                        Calendar c = Calendar.getInstance();
//                        SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd");    // HH:mm:ss
//                        String strDate = formatter.format(c.getTime());
                        String statTime = "";
                        String endTime = "";
                        for (int j = 0; j < stations.size(); j++) {
                            if (chooseStartId.equals(stations.get(j).getStationId())){
                                statTime = stations.get(j).getTime();
                            }
                            if (chooseEndId.equals(stations.get(j).getStationId())){
                                endTime = stations.get(j).getTime();
                            }
                        }
//                        String[] datas = new String[checkTickets.size()];
//                        for (int i = 0; i < checkTickets.size(); i++) {
//                            datas[i] = checkTickets.get(i).getTotalDate();
//                        }
                        List<String> datas = new ArrayList<>();
                        for (int i = 0; i < checkTickets.size(); i++) {
                            datas.add(checkTickets.get(i).getTotalDate());
                        }
                        presenter.doBuyTicketOnOnce(BuyTicketActivity.this,SPUtils.getUserPhone(getApplicationContext())
                                ,travelLineBean.getNumber(), chooseStartId
                                ,chooseEndId,datas,statTime,endTime,number);
                    }
                }else{
//                    //购买月票
//                    String[] thisTime = TimeRender.getFullDateStr9();
//                    String statTime = "";
//                    String endTime = "";
//                    for (int j = 0; j < stations.size(); j++) {
//                        if (chooseStart.equals(stations.get(j).getName())){
//                            statTime = stations.get(j).getTime();
//                        }
//                        if (chooseEnd.equals(stations.get(j).getName())){
//                            endTime = stations.get(j).getTime();
//                        }
//                    }
//                    presenter.doBuyTicketOnMonth(BuyTicketActivity.this,SPUtils.getUserPhone(getApplicationContext()),
//                            travelLineBean.getNumber(),thisTime[1],thisTime[0],chooseStart,chooseEnd,
//                            statTime,endTime);
                }
            }
        });

    }


    @Override
    protected BasePresenter bindPresenter() {
        return presenter;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.head_left:
                finish();
                break;
            case R.id.location_start:
                if (startStationsId.size()==0 && stations.size()>0){
                    for (int i = 0; i < stations.size()-1; i++) {
                        startStations.add(stations.get(i).getName());
                        startStationsId.add(stations.get(i).getStationId());
                    }
                }
                tvStart.show(v);
                break;
            case R.id.location_end:
                if (chooseStartId.equals("")){
                    ToastJoe.getmToastJoe().ToastLongShow(BuyTicketActivity.this,null,"请先选择出发站");
                }else {
                    ClearEndStation();
                    tvEnd.show(v);
                }
                break;
//            case R.id.buyticket_checkline1:
//                if (!box1.isChecked()){
//                    box1.setChecked(true);
//                    box2.setChecked(false);
//                }
//                break;
//            case R.id.buyticket_checkline2:
//                if (!box2.isChecked()){
//                    if (chooseStartId.equals("")){
//                        ToastJoe.getmToastJoe().ToastShow(BuyTicketActivity.this,null,"请选择上车站点");
//                        box1.setChecked(true);
//                        box2.setChecked(false);
//                    }else if (chooseEndId.equals("")){
//                        box1.setChecked(true);
//                        box2.setChecked(false);
//                        ToastJoe.getmToastJoe().ToastShow(BuyTicketActivity.this,null,"请选择下车站点");
//                    }
//                    else if (endStationsId.contains(chooseStartId)){
//                        box1.setChecked(true);
//                        box2.setChecked(false);
//                        ToastJoe.getmToastJoe().ToastShow(BuyTicketActivity.this,null,"上车站点不能再下车站点之后");
//                        end.setText("");
//                        chooseEnd="";
//                    }
//                    else {
//                        box2.setChecked(true);
//                        box1.setChecked(false);
//                    }
//
//                }
//                break;
            case R.id.ticket_once_about:
                Intent intent = new Intent(BuyTicketActivity.this,InfoActivity.class);
                intent.putExtra("type",1);
                startActivity(intent);
                break;

            case R.id.add:
                if (checkTickets.size()>0) {
                    if (number > 1) {
                        perNumber.setText((--number) + "");
                    }
                }else{
                    ToastJoe.getmToastJoe().ToastShow(BuyTicketActivity.this,null,"请先选择乘车时间");
                }
                break;
            case R.id.low:
                if (checkTickets.size()>0) {
                    if (number<10) {
                        perNumber.setText((++number) + "");
                    }else{
                        ToastJoe.getmToastJoe().ToastShow(BuyTicketActivity.this,null,"一次最多选择十人哦~");
                    }
                }else{
                    ToastJoe.getmToastJoe().ToastShow(BuyTicketActivity.this,null,"请先选择乘车时间");
                }
                break;
        }
    }

    private void ClearEndStation(){
        endStations.clear();
        endStationsId.clear();
        for (int i = postition+1; i < stations.size(); i++) {
            endStations.add(stations.get(i).getName());
            endStationsId.add(stations.get(i).getStationId());
        }
    }

    @Override
    public void showLoading() {
        if (waitDialog==null)
            waitDialog = new LoadingDialog(BuyTicketActivity.this);
        if (!waitDialog.isShowing())
            waitDialog.show();
    }

    @Override
    public void closeLoading() {
        if (waitDialog!=null && waitDialog.isShowing())
            waitDialog.dismiss();
    }

    @Override
    public void loadError(String msg) {
        ToastJoe.getmToastJoe().ToastLongShow(BuyTicketActivity.this,null,msg);
    }

    @Override
    public void loadedData(List<StationBean> s) {
        stations  = s;
        if (startStationsId.size()==0 && stations.size()>0){
            for (int i = 0; i < stations.size()-1; i++) {
                startStations.add(stations.get(i).getName());
                startStationsId.add(stations.get(i).getStationId());
            }
            for (int i = postition+1; i < stations.size(); i++) {
                endStations.add(stations.get(i).getName());
                endStationsId.add(stations.get(i).getStationId());
            }
        }
        chooseStart = stations.get(0).getName();
        chooseStartId = stations.get(0).getStationId();
        chooseEnd = stations.get(stations.size()-1).getName();
        chooseEndId = stations.get(stations.size()-1).getStationId();
        start.setText(chooseStart);
        end.setText(chooseEnd);
        presenter.startGetPrice(travelLineBean.getNumber(), chooseStartId, chooseEndId);
    }

    @Override
    public void loadeIsCanBuyTicket(String isOk,DateJoe dateJoe,int pos) {
        Log.i("taylor000",dateJoe.getTotalDate()+" "+stations.get(0).getTime());
//        if (TimeRender.compareTime666(dateJoe.getTotalDate()+" "+stations.get(0).getTime())){
//            dateJoe.isCheck = false;
//            adapter.notifyItemChanged(pos,dateJoe);
//            Log.i("taylor6666",isOk);
//            ToastJoe.getmToastJoe().ToastLongShow(BuyTicketActivity.this,null,"该班次已发车");
//        }else{
            if (isOk.equals("false")){
                dateJoe.isCheck = false;
                Log.i("taylor99999",isOk);
                adapter.notifyItemChanged(pos,dateJoe);
//            ToastJoe.getmToastJoe().ToastLongShow(BuyTicketActivity.this,null,"该车票已售罄");
            }
//        }

    }

    @Override
    public void loadeIsCanBuyTicket1(int isOk, DateJoe dateJoe, int pos ,String count) {
//        if (isOk>0){
//
//        }else{
//            loadError("该车票已售罄");
//        }

        if (TimeRender.compareTime666(dateJoe.getTotalDate()+" "+stations.get(stations.size()-1).getTime())){
            dateJoe.isCheck = false;
            adapter.notifyItemChanged(pos,dateJoe);
            ToastJoe.getmToastJoe().ToastLongShow(BuyTicketActivity.this,null,"该班次已收车");
        }else {
            dateJoe.setCount(count);
            adapter.notifyItemChanged(pos, dateJoe);
            checkTickets.add(dateJoe);
            ChangeToatalMoney();
        }

    }

    private void ChangeToatalMoney(){
        if (price!=null && !price.equals("")) {
            if (isMonthTicket) {
//                String[] thisTime = TimeRender.getFullDateStr9();
//                if (thisTime[1].equals("1") || thisTime[1].equals("3")|| thisTime[1].equals("5")|| thisTime[1].equals("7")
//                        || thisTime[1].equals("8")|| thisTime[1].equals("10")|| thisTime[1].equals("12")){
//                    month_price.setText("￥" + (31 * Double.valueOf(price)));
//                    total_price.setText("￥" + (31 * Double.valueOf(price)));
//                }else if (thisTime[1].equals("2")){
//                    int year= Integer.valueOf(thisTime[0]);
//                    if ((year%4==0 && year%100!=0) || (year%400==0)){
//                        month_price.setText("￥" + (29 * Double.valueOf(price)));
//                        total_price.setText("￥" + (29 * Double.valueOf(price)));
//                    }else{
//                        month_price.setText("￥" + (28 * Double.valueOf(price)));
//                        total_price.setText("￥" + (28 * Double.valueOf(price)));
//                    }
//                }else{
//                    month_price.setText("￥" + (30 * Double.valueOf(price)));
//                    total_price.setText("￥" + (30 * Double.valueOf(price)));
//                }
//                once_price.setText("￥" + price + "/次");
            } else {
                int num = checkTickets.size()*number;
                total_price.setText("￥" + (num * Double.valueOf(price)));
                checkTicketNumber.setText("共选择" + num + "张车票");
            }
        }else{
            ToastJoe.getmToastJoe().ToastLongShow(BuyTicketActivity.this,null,"线路信息错误");
            finish();
        }

    }

    @Override
    public void loadeBuyPrice(String price) {
        if (travelLineBean.getIsUserAllPrice()!=null){
            Log.i("sb",travelLineBean.getIsUserAllPrice());
            if(travelLineBean.getIsUserAllPrice().equals("0")){
                if (price!=null && !price.equals("0")){
                    this.price = price;
                }
            }else{
                this.price = travelLineBean.getPrice();
            }
            for (int i = 0; i < dateJoes.size(); i++) {
                dateJoes.get(i).setPrice("￥"+this.price);
            }
            adapter.setData(dateJoes);
            adapter.notifyDataSetChanged();
            ChangeToatalMoney();
        }

    }

    @Override
    public void loadeIsBuySuccess(RequestBean requestBea) {
        closeLoading();
        if (requestBea.getReCode().equals("201")){
            Intent intent = new Intent(BuyTicketActivity.this,PayActivity.class);
            Bundle bundle = new Bundle();
            bundle.putInt("position",0);
            bundle.putSerializable("bean",travelLineBean);
            bundle.putSerializable("list", (Serializable) checkTickets);
            bundle.putString("start", chooseStart+"("+stations.get(startStationsId.indexOf(chooseStartId)).getTime()+")");
//            bundle.putString("start", stations.get(0).getName()+"("+stations.get(0).getTime()+")");
//            bundle.putString("end", stations.get(stations.size()-1).getName()+"("+stations.get(stations.size()-1).getTime()+")");
            bundle.putString("end", chooseEnd+"("+stations.get(endStationsId.indexOf(chooseEndId)).getTime()+")");
            bundle.putString("price",price);
            bundle.putSerializable("pay",requestBea);
            bundle.putInt("number",number);
            intent.putExtras(bundle);
            startActivity(intent);
        }else if (requestBea.getReCode().equals("202")){
            // TODO: 2018/7/2 这里跳转到车票界面展示周期票
            ToastJoe.getmToastJoe().ToastShowRight(getApplicationContext(),null,"购买成功");
            Intent intent = new Intent(BuyTicketActivity.this, MyTicketActivity.class);
            startActivity(intent);
            BuyTicketActivity.activity.finish();
            finish();
        }else{
            ToastJoe.getmToastJoe().ToastLongShow(this,null,requestBea.getCodeTxt());
        }
    }

    @Override
    public void loadBuyMonthSuccess(RequestBean requestBean) {
        closeLoading();
        if (requestBean.getReCode().equals("201")){
            Intent intent = new Intent(BuyTicketActivity.this,PayActivity.class);
            Bundle bundle = new Bundle();
            bundle.putInt("position",1);
            bundle.putSerializable("bean",travelLineBean);
            bundle.putSerializable("list", (Serializable) checkTickets);
            bundle.putString("start", stations.get(0).getName()+"("+stations.get(0).getTime()+")");
            bundle.putString("end", stations.get(stations.size()-1).getName()+"("+stations.get(stations.size()-1).getTime()+")");
            bundle.putString("price",total_price.getText().toString().replace("￥",""));
            bundle.putSerializable("pay",requestBean);
            bundle.putInt("number",number);
            intent.putExtras(bundle);
            startActivity(intent);
        }else{
            ToastJoe.getmToastJoe().ToastLongShow(this,null,requestBean.getCodeTxt());
        }
    }

    @Override
    public void loadDays(List<YuPiaoBean> beans) {
        if (beans!=null && beans.size()>0  && dateJoes.size()>0){
            for (int i = 0; i < dateJoes.size(); i++) {
                for (int j = 0; j < 21; j++) {
                    if (beans.get(j).getBusTime()!=null && !beans.get(j).getBusTime().equals("")){
                        int day = Integer.valueOf(beans.get(j).getBusTime().split("\\.")[2]);
                        if (day==Integer.valueOf(dateJoes.get(i).getDay())){
                            if (beans.get(j).getYuPiao()<=0){
                                dateJoes.get(i).setSlodOut(true);
                                dateJoes.get(i).setCount(0+"");
                                dateJoes.get(i).isShow = false;
                            }else{
                                dateJoes.get(i).isShow = true;
                                dateJoes.get(i).setSlodOut(false);
                                dateJoes.get(i).setCount(beans.get(j).getYuPiao()+"");
                            }
                            break;
                        }
                    }
                }
                if (travelLineBean.getType()!=null && travelLineBean.getType().equals("CommuteLine")){
                    dateJoes.get(i).setCommuting(true);
                    if (dateJoes.get(i).getWeek()==0 || dateJoes.get(i).getWeek()==6){
                        dateJoes.get(i).isShow = false;
                    }
                }
                if (dateJoes.get(i).getCount()==null || dateJoes.get(i).getCount().equals("")){
                    dateJoes.get(i).setCount(beans.get(0).getAllSeat()+"");
                }
            }
            adapter.setData(dateJoes);
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void getBuyeds(List<BuyedBean> beans) {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(waitDialog!=null){
            waitDialog.dismiss();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("购票");
        MobclickAgent.onResume(this); //统计时长

    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("购票");
        MobclickAgent.onPause(this);
    }
}
